// pages/tijiao_record/tijao_record.js
var t = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
	cb:false,
    state:0,
    num:1,
    cardId:'',
    phone:'',
   
    detail:'',
    // 页面价格
    price:10,
    // 保留价格
    proce: "",
    danjia:0,
    ticketId:"",
    peopletype:"",
    ticketname:"",
    isNeedAddress:"",
    isNeedIdnumber:"",
    memberId:"",
    couponId:"",
    // 优惠卷
    coupons:[],
    indexid:""
  },
  cbs:function(e){
	  this.cb=!this.cb
	  console.log(this.cb)
  },
  addres:function(){
      wx.navigateTo({
        url: '../addres_list/addres_list',
      })
  },
  reduct:function(){
    var danprice = this.data.danjia
    var num = this.data.num
    if(num >1){
        num-=1
      var price = (num * danprice).toFixed(2)
        this.setData({
          num:num,
          price: price
        })
    }
    if (num == 1){
      var danprice = this.data.danjia
      this.setData({
        num: num,
        price: danprice
      })
    }
  },
  add: function () {
    var danprice = this.data.danjia
    var num = this.data.num
      num += 1
    var price = (num * danprice).toFixed(2)
      this.setData({
        num: num,
        price:price
      })
  },
  // 身份证校验
  cardId: function (e) {
    this.setData({ cardId:e.detail.value})},
    phone:function(e){this.setData({phone:e.detail.value})},
  jiaoyan:function(carId){
    if (!(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(carId))) {
      wx.showToast({

        title: '身份证号码有误',

        duration: 2000,

        icon: 'none'

      });

      return false;

    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that=this;
    that.setData({
      price: options.money,
      proce: options.money,
      danjia: options.money,
      ticketId: options.ticketId,
      peopletype: options.peopletype,
      ticketname: options.ticketname,
      isNeedAddress: options.isNeedAddress,
      isNeedIdnumber: options.isNeedIdnumber,
      memberId: wx.getStorageSync('memberInfo').id
    })


  },
  //查看可用优惠券
  chakan: function () {
    
   
  },

  formSubmit: function (t) {
    var that=this
    var visitName = t.detail.value.visitName;

    var visitPhone = t.detail.value.visitPhone;

    // mobile

    var visitIdnumber = t.detail.value.visitIdnumber;
    //收货地址是否必填
    var isNeedAddress = that.data.isNeedAddress

    //身份证号是否必填

    var isNeedIdnumber = that.data.isNeedIdnumber

    
    var tel = /^1[34578]\d{9}$/

    
    if (isNeedAddress=='1' && this.data.detail.name==null){
      wx.showToast({
        title: '请选择收货地址',
        icon: "none",
        duration: 2e3
      });
      return false
     } else if (visitName == "" ) {
       wx.showToast({
         title: '请填写姓名',
         icon: "none",
         duration: 2e3
       });
      return false

     } 
    else if (visitPhone == "") {
      wx.showToast({
        title: '请填写手机号',
        icon: "none",
        duration: 2e3
      });
      return false

    } 
    else if(!tel.test(this.data.phone)) {
      wx.showToast({
        title: '请输入正确手机号！',
        icon: 'none'
      })
      return false
    }
    else if (isNeedIdnumber=="1" && visitIdnumber == "") {
      wx.showToast({
        title: '请填写身份证号',
        icon: "none",
        duration: 2e3
      });
      return false

    } else if (isNeedIdnumber == "1" && !(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(visitIdnumber))){
      wx.showToast({
        title: '身份证号码有误',

        duration: 2000,

        icon: 'none'

      });
      
      return false
    }
    
   
    console.log(t)
    console.log("12121212121212121212121212121212")
    console.log("form发生了submit事件，携带数据为：", t.detail.value), this.overbooking(t.detail.value);
    
  },
//保存订单
  overbooking: function (o) {
    var a = this;
    wx.showLoading({
      title: '支付中',
      mask: true
    })
    wx.request({
      url: t.globalData.appletUrl + "/applet/generateorder",
      data: o,
      header: {
        "content-type": "application/json"
      },
      success: function (t) {
        console.log(t.data);
        // a.wxaPay(t.data.data.id);
        if(t.data.code=='000000'){//保存订单失败
          a.wxaPay(t.data.data.id);
         
        }else{
          wx.showToast({
            title: '提交成功',
            icon: "none",
            duration: 2e3
          });

          console.log(t.data.msg);
        }
      }
    });
  },

  // 发起支付
  wxaPay: function (orderId) {
    var that=this
    wx.request({
      url: t.globalData.appletUrl + '/applet/orderPay',
      data: {
        orderId: orderId,
        openId: wx.getStorageSync('openId')
      },
      success: function (res) {
        console.log(res)
        wx.requestPayment({
          timeStamp: res.data.data.timeStamp,
          nonceStr: res.data.data.nonceStr,
          package: res.data.data.package,
          signType: res.data.data.signType,
          paySign: res.data.data.paySign,
          success: function (res) {
            console.log(res);
            setTimeout(function () {
              wx.hideLoading()  //showLoading 只能用此语句关闭
            }, 700)
           wx.navigateTo({
             url: '../accom/accom?price=' + that.data.price,
           })
          }
          ,'fail': function (res) {
            console.log('失败')
            console.log(res)
            wx.showToast({
              title: '支付失败！',
              icon: 'none'
            })
            // that.setData({
            //   balanceRecordid: da.balanceRecordid
            // })
            // that.setData({
            //   // balanceRecordid: da.balanceRecordid
            // })
            that.fail()
          },
          'complete': function (res) {
            console.log('完整的')
            console.log(res)
          }
        })
      }
    })
  },
  // 跳转优惠卷页面
  youhuijuan:function(){
    var that=this
    wx.request({
      url: t.globalData.appletUrl+ '/applet/availableCoupons',
      data:{
        ticketId: that.data.ticketId,
        memberId: that.data.memberId,
        money:'1000'
      },
      success(res){
        console.log(res)
        that.setData({
          coupons:res.data.data,
          indexid: res.data.data[0].memberId
        })
      }
    })
  },
  // 使用优惠券
  discount:function(e){
    var that=this
    console.log(e)
    var discount = e.currentTarget.dataset.cardid
    var money = e.currentTarget.dataset.money
    var couponId1 = e.currentTarget.dataset.coupon
    console.log(couponId1)
    this.setData({
      indexid: discount
    })
    // 金额减去优惠卷
    var price = that.data.proce
    var price1=price-money
    this.setData({
      price:price1,
      couponId: couponId1
    })

  },
  openSuccess: function (t) {
    console.log(t), wx.navigateTo({
      // url: "../preorder/msg_success"
      url: "../cd/cd?shopId=" + t
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  tijiao:function(){
    // if(this.data.cardId =='' || this.data.phone == ''){
    //     wx.showToast({
    //       title: '请完善信息！',
    //       icon:'none'
    //     })
    //     return
    // }
    
  }
})